package com.epower.base;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.ali.luna.commons.util.PageInfo;

public class BaseDO extends PageInfo {
	private static final long serialVersionUID = 741231858441822688L;

	/* 扩展基础分页排序，主要用于点击页面标题实现的单列排序*/

	private String orderColumn;

	private boolean orderAsc = false;

	/**
	 * @return
	 * @see java.lang.Object#toString()
	 */
	@SuppressWarnings("unchecked")
	public String toString() {
		try {
			Map props = BeanUtils.describe(this);
			Iterator iProps = props.keySet().iterator();
			StringBuffer retBuffer = new StringBuffer();

			while (iProps.hasNext()) {
				String key = (String) iProps.next();

				// skip false property "class"
				if ("class".equals(key)) {
					continue;
				}

				retBuffer.append(key).append("=[").append(props.get(key)).append("]");

				if (iProps.hasNext()) {
					retBuffer.append(", ");
				}
			}

			return retBuffer.toString();
		} catch (Exception e) {
			return super.toString();
		}
	}

	public Integer getStartRow() {
		if (super.getStartRow() > 0) {
			return super.getStartRow() - 1;
		}
		return super.getStartRow();
	}

	@Override
	public Integer getEndRow() {
		return super.getEndRow() - 1;
	}

	/**
	 * 扩展
	 */
	public int getToPage() {
		int toPage = super.getToPage();
		if (this.getTotalItem() > 0 && toPage < 1) {
			toPage = 1;
		}
		return toPage;
	}

	/**
	 * 扩展
	 * @return
	 */
	public boolean isFirstPage() {
		if (this.getTotalItem() == 0)
			return true;
		return super.isFirstPage();
	}

	/**
	 * 扩展
	 * @return
	 */
	public boolean isLastPage() {
		if (this.getTotalItem() == 0)
			return true;
		return super.isLastPage();
	}

	public String getOrderColumn() {
		return orderColumn;
	}

	public void setOrderColumn(String orderColumn) {
		this.orderColumn = orderColumn;
	}

	public boolean isOrderAsc() {
		return orderAsc;
	}

	public void setOrderAsc(boolean orderAsc) {
		this.orderAsc = orderAsc;
	}

}
